<template>
    <div>
        <Modal
                v-model="teachingModal"
                title="添加排课"
                :closable="false"
                :mask-closable="false">
            <Form
                    ref="formValidate"
                    :model="formItem"
                    :rules="ruleValidate"
                    :label-width="80">
                <FormItem label="上课周次" prop="week">
                    <Select v-model="formItem.week">
                        <Option v-for="item in weekList" :value="item" :key="item">{{ item }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="上课节次" prop="time">
                    <Input v-model="formItem.time" placeholder="上课节次"/>
                </FormItem>
                <FormItem label="教室" prop="room">
                    <Input v-model="formItem.room" placeholder="教室"/>
                </FormItem>
                <FormItem label="年级" prop="grade">
                    <Input v-model="formItem.grade" placeholder="年级"/>
                </FormItem>
                <FormItem label="班型" prop="type">
                    <Input v-model="formItem.type" placeholder="班型"/>
                </FormItem>
                <FormItem label="上课学生" prop="student">
                    <Input v-model="formItem.student" type="textarea" placeholder="上课学生，以逗号隔开"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="cancel">取消</Button>
                <Button type="primary" size="large" @click="submit('formValidate')">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {saveTeaching} from "../request/api"

    export default {
        name: "TeacherForm",
        data() {
            return {
                formItem: '',
                weekList: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
                ruleValidate: {
                    week: [
                        {required: true, message: '周次不能为空', trigger: 'blur'}
                    ],
                    time: [
                        {required: true, message: '时间不能为空', trigger: 'blur'}
                    ],
                    room: [
                        {required: true, message: '角色不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        props: ['teachingModal', 'modify'],
        watch: {
            modify: function () {
                this.formItem = this.modify;
            }
        },
        methods: {
            cancel() {
                this.$parent.cancelTeachingForm()
            },
            submit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        saveTeaching(this.formItem).then((res) => {
                            if (this.formItem.id != '') {
                                this.$parent.cancelTeachingForm();
                            }
                            if (res.code == 200) {
                                this.$Message.success('保存成功');
                                this.$refs[name].resetFields();
                                this.$parent.teachingList();
                            } else
                                this.$Message.error(res.msg);
                        })
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>
